layui.use(['layer','element'],function (){
    let layer = layui.layer,element = layui.element;

});

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/17
* @description: 页面初始化生成导航
*/
$(function (){
    // 初始化单位标题跟用户名称，改用 SpringMVC 的 modelMap 传值
    //selectUserInfo();
    //queryIndexTitle();
    getMenuList();
    showTable('F100000','/userinfo/home','首页');
})

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/17
* @description: 生成导航菜单
*/
function getMenuList(){
    sendAjax("/index/menu",null,function (result) {
        let codeV = result.code;
        if (valueIsEqual("00",codeV)){
            let html1 = "";
            let html2 = "";
            let menuVoLength = result.data.length;
            //判断是否包含菜单信息
            if (menuVoLength > 0){
                for (let i=0;i<menuVoLength;i++){
                    //获取一级菜单信息
                    let menuVoFir = result.data[i];
                    if (valueIsEqual(menuVoFir.menuDesc,"用户信息")){
                        //头部菜单 -> 遍历二级菜单集合
                        let menuVoSecoList = menuVoFir.menuVOList;
                        let menuVoSencLength = menuVoSecoList.length;
                        for (let j=0;j<menuVoSencLength;j++){
                            let menuVoSeco = menuVoSecoList[j];
                            if (valueIsEqual(menuVoSeco.menuDesc,"主页面") || valueIsEqual(menuVoSeco.menuDesc,"首页")){
                                continue;
                            }
                            html1 += '<dd><a href=\"javascript:;\" onclick=\"openTabUserInfo(\''+menuVoSeco.menuId+'\',\''
                                +menuVoSeco.menuUrl+'\',\''+menuVoSeco.menuDesc+'\')\"><span>'+menuVoSeco.menuDesc+'</span></a></dd>';
                        }
                    }else {
                        html2 += '<li class="layui-nav-item">'+'<a href=\"javascript:;\">'
                            +getMenuImg(i)+'<span class="side_div_ul_li_a_span">'+menuVoFir.menuDesc+'</span></a>'
                            +'<dl class=\"layui-nav-child \">';

                        let menuVoSecoList = menuVoFir.menuVOList;
                        let menuVoSencLength = menuVoSecoList.length;
                        for (let j=0;j<menuVoSencLength;j++){
                            let menuVoSeco = menuVoSecoList[j];
                            html2 += '<dd><a href=\"javascript:;\" onclick=\"showTable(\''+menuVoSeco.menuId+'\',\''
                                +menuVoSeco.menuUrl+'\',\''+menuVoSeco.menuDesc+'\')\"><span class="side_div_ul_li_dl_dd_span">'+menuVoSeco.menuDesc+'</span></a></dd>';
                        }
                        html2 += "</dl></li>"
                    }
                }
            }
            $(".header_ul_li_dl").append(html1);
            $(".side_div_ul").append(html2);
            //重新渲染 element
            layui.element.render();
        }
    })
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/9/18
* @description: 一级菜单小图标
*/
function getMenuImg(index){
    let imgS = [
        '<i class=\"layui-icon layui-icon-survey  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-read  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-survey  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-tabs  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-form  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-chart-screen  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-set-fill  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-util  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-file  body_div_ul_li_a_i\" ></i>',
        '<i class=\"layui-icon layui-icon-list  body_div_ul_li_a_i\" ></i>'
    ];
    return imgS[index];
}

/**
* @author: ChengJW
* @params:  id 菜单编号，url 菜单指向的地址， name 菜单名称
* @return:
* @date: 2021/9/18
* @description: 点击左侧菜单，打开相应的 tab 并展示
*/
function showTable(id,url,name) {
    //获取tab中的所有li，并遍历，如果存在相同id，则说明该菜单已经存在，则切换到该标签
    let liList = $(".body_div_ul").find("li");

    // 1、第一种显示 tabs 的样式，横线显示多个 tabs
    /*let isTrue = false;
    layui.each(liList,function (){
        let layId = $(this).attr("lay-id");
        if(valueIsEqual(layId,id)){
           isTrue = true;
        }
    });
    if (isTrue){
        layui.element.tabChange('indexTable',id);
        layui.element.render();
    }else {
        layui.element.tabAdd('indexTable', {
            title: name,
            content: '<iframe class="body_div_div_iframe" data-frame id=\"' + id + '\" src=\"' + url + '\" ></iframe>',
            id: id
        });
        layui.element.tabChange('indexTable', id);
        layui.element.render();
    }*/

    // 2、第二种显示 tabs 方式，只显示一个
    layui.each(liList,function (){
        let layId = $(this).attr("lay-id");
        if(!valueIsEqual(layId,"F100000")){
            layui.element.tabDelete("indexTable",layId);
        }
    });
    //当点击导航栏中的首页菜单时，如果菜单已经存在，则跳至菜单页，如果没有就新增；初始化页面时，新增首页 tab
    if (valueIsEqual(id,"F100000") && liList.length > 0){
        layui.element.tabChange('indexTable',id);
        layui.element.render();
    }else {
        layui.element.tabAdd('indexTable', {
            title: name,
            content: '<iframe class="body_div_div_iframe" data-frame id=\"' + id + '\" src=\"' + url + '\" ></iframe>',
            id: id
        });
        layui.element.tabChange('indexTable', id);
        layui.element.render();
    }
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/7
* @description:
*/
function updateUserPhoto() {
    sendAjax("/index/userInfoVO",null,function (result) {
       let userinfo = result.data;
       if (valueIsEqual(result.code,"00")){
           $(".layui-nav-img").attr("src", userinfo.urPhotoPath);
       }
    });
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/20
* @description: 退出
*/
function logout() {
    layui.layer.confirm("<span style='font-weight: bolder;color: red'>Are you sure to logout the system ？</span>",
        {btn:['Sure','Quit'],icon:2,title: '<span style=\'font-weight: bolder;color: red\'>Notice</span>'},
        function (index) {
        sendAjax("/index/logout",null,function (result) {
            let userinfo = result.data;
            if (valueIsEqual(result.code,"00")){
                window.location.href = "/login";
            }else if (valueIsEqual(result.code,"01")){
                layer.alert("退出失败，请稍后再试",{icon:3,title:'Notice'});
            }
        })
    });
}

/**
* @author: ChengJW
* @params:
* @return:
* @date: 2021/11/20
* @description: ID 通过与后端约定获取
*/
function openTabUserInfo(id,url,name) {
    //修改用户头像权限id
    let updatePhotoUrId = 'P1630283268979';
    //修改用户密码权限id
    let updatePswUrId = 'P1630283268980';
    //修改用户信息的权限Id
    let updateInfoId = 'P1630283268981';
    if (valueIsEqual(id,updatePhotoUrId)){
        layui.layer.open({
            title:'修改头像',
            type:2,
            resize:false,
            area:['500px','350px'],
            content: '/userinfo/updateProfilePhoto',
            end:function (obj) {
                updateUserPhoto();
            }
        });
    }else if (valueIsEqual(id,updatePswUrId)){
        layui.layer.open({
            title:'修改密码',
            type:2,
            resize:false,
            area:['500px','300px'],
            content: '/userinfo/updatePassword',
            end:function (obj) {
            }
        });
    }else if (valueIsEqual(id,updateInfoId)){
        layui.layer.open({
            title:'修改用户信息',
            type:2,
            resize:false,
            area:['500px','450px'],
            content: '/userinfo/updateUserinfo',
            end:function (obj) {
            }
        });
    }
}
